//
// Created by Wirano on 2020/11/13.
//

#include "Drv_Usart.h"
#include "Ano_DT.h"
#include "Ano_OF.h"
#include "Drv_OpenMV.h"
#include "include.h"
#include "Drv_UP_Flow.h"
//====uart2
extern UART_HandleTypeDef huart2;

void Usart2_Init(uint32_t br_num)
{
    huart2.Instance = USART2;
    huart2.Init.BaudRate = br_num;
    huart2.Init.WordLength = UART_WORDLENGTH_8B;
    huart2.Init.StopBits = UART_STOPBITS_1;
    huart2.Init.Parity = UART_PARITY_NONE;
    huart2.Init.Mode = UART_MODE_TX_RX;
    huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart2.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart2) != HAL_OK)
    {
        Error_Handler();
    }

    //使能USART2接收中断
    USART2->CR1 |= USART_CR1_RXNEIE;
//    //使能USART2
//    USART_Cmd ( USART2, ENABLE );
//	//使能发送（进入移位）中断
    if (!(USART2->CR1 & USART_CR1_TXEIE))
    {
        USART2->CR1 |= USART_CR1_TXEIE;
    }

    __HAL_UART_ENABLE_IT(&huart2,UART_IT_ERR);
}

uint8_t TxBuffer[256];
uint8_t TxCounter = 0;
uint8_t count = 0;

uint8_t Rx_Buf[256];    //串口接收缓存

void Usart2_IRQ(void)
{
    uint8_t com_data;

    if (USART2->SR & USART_SR_ORE) //ORE中断
    {
        com_data = USART2->DR;
    }

    //接收中断
    if (USART2->SR & USART_SR_RXNE)
    {
        USART2->SR &= ~USART_SR_RXNE; //清除中断标志

        com_data = USART2->DR;
        ANO_DT_Data_Receive_Prepare(com_data);
    }
    //发送（进入移位）中断
    if (USART2->SR & USART_SR_TXE)
    {

        USART2->DR = TxBuffer[TxCounter++]; //写DR清除中断标志
        if (TxCounter == count)
        {
            USART2->CR1 &= ~USART_CR1_TXEIE;        //关闭TXE（发送中断）中断
        }

        //USART_ClearITPendingBit(USART2,USART_IT_TXE);
    }
}

void Usart2_Send(unsigned char *DataToSend, uint8_t data_num)
{
    uint8_t i;
    for (i = 0; i < data_num; i++)
    {
        TxBuffer[count++] = *(DataToSend + i);
    }

    if (!(USART2->CR1 & USART_CR1_TXEIE))
    {
        USART2->CR1 |= USART_CR1_TXEIE; //打开发送中断
    }
}


//====uart3
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

extern UART_HandleTypeDef huart3;

void Usart3_Init(uint32_t br_num)
{
    huart3.Instance = USART3;
    huart3.Init.BaudRate = br_num;
    huart3.Init.WordLength = UART_WORDLENGTH_8B;
    huart3.Init.StopBits = UART_STOPBITS_1;
    huart3.Init.Parity = UART_PARITY_NONE;
    huart3.Init.Mode = UART_MODE_TX_RX;
    huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart3.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart3) != HAL_OK)
    {
        Error_Handler();
    }

    //使能USART3接收中断
    USART3->CR1 |= USART_CR1_RXNEIE;
    //使能USART3
//    USART_Cmd(USART3, ENABLE);
    __HAL_UART_ENABLE_IT(&huart3,UART_IT_ERR);
}

uint8_t Tx3Buffer[256];
uint8_t Tx3Counter = 0;
uint8_t count3 = 0;

void Usart3_IRQ(void)
{
    uint8_t com_data;

    if (USART3->SR & USART_SR_ORE) //ORE中断
        com_data = USART3->DR;

    //接收中断
    if (USART3->SR & USART_SR_RXNE)
    {
        USART3->SR &= ~USART_SR_RXNE; //清除中断标志
        com_data = USART3->DR;
//      Ano_UWB_Get_Byte(com_data);
        OpenMV_Byte_Get(com_data);
    }
    //发送（进入移位）中断
    if (USART3->SR & USART_SR_TXE)
    {
        USART3->DR = Tx3Buffer[Tx3Counter++]; //写DR清除中断标志
        if (Tx3Counter == count3)
        {
            USART3->CR1 &= ~USART_CR1_TXEIE;        //关闭TXE（发送中断）中断
        }
    }
}

static void Usart3_Send(unsigned char *DataToSend, uint8_t data_num)
{
    uint8_t i;
    for (i = 0; i < data_num; i++)
    {
        Tx3Buffer[count3++] = *(DataToSend + i);
    }
    if (!(USART3->CR1 & USART_CR1_TXEIE))
    {
        USART3->CR1 |= USART_CR1_TXEIE; //打开发送中断
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//====uart4

extern UART_HandleTypeDef huart4;

void Uart4_Init(uint32_t br_num)
{
    huart4.Instance = UART4;
    huart4.Init.BaudRate = br_num;
    huart4.Init.WordLength = UART_WORDLENGTH_8B;
    huart4.Init.StopBits = UART_STOPBITS_1;
    huart4.Init.Parity = UART_PARITY_NONE;
    huart4.Init.Mode = UART_MODE_TX_RX;
    huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart4.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart4) != HAL_OK)
    {
        Error_Handler();
    }

    //使能UART5接收中断
    UART4->CR1 |= USART_CR1_RXNEIE;
    //使能USART5
//    USART_Cmd(UART4, ENABLE);
    __HAL_UART_ENABLE_IT(&huart4,UART_IT_ERR);
}

uint8_t Tx4Buffer[256];
uint8_t Tx4Counter = 0;
uint8_t count4 = 0;

void Uart4_IRQ(void)
{
    uint8_t com_data;

    //接收中断
    if (UART4->SR & USART_SR_RXNE)
    {
        UART4->SR &= ~USART_SR_RXNE; //清除中断标志

        com_data = UART4->DR;
        //====
        //匿名光流解析
        if (of_init_type != 2)
        {
            AnoOF_GetOneByte(com_data);//匿名光流
        }
        //优像光流解析
        if (of_init_type != 1)
        {
            OFGetByte(com_data);//优像光流
        }
//		if(LASER_LINKOK)
//			Drv_Laser_GetOneByte( com_data);
//		else
//			AnoOF_GetOneByte ( com_data );
    }

    //发送（进入移位）中断
    if (UART4->SR & USART_SR_TXE)
    {

        UART4->DR = Tx4Buffer[Tx4Counter++]; //写DR清除中断标志

        if (Tx4Counter == count4)
        {
            UART4->CR1 &= ~USART_CR1_TXEIE;        //关闭TXE（发送中断）中断
        }

        //USART_ClearITPendingBit(USART2,USART_IT_TXE);
    }
}

void Uart4_Send(unsigned char *DataToSend, uint8_t data_num)
{
    uint8_t i;
    for (i = 0; i < data_num; i++)
    {
        Tx4Buffer[count4++] = *(DataToSend + i);
    }

    if (!(UART4->CR1 & USART_CR1_TXEIE))
    {
        UART4->CR1 |= USART_CR1_TXEIE; //打开发送中断
    }

}

//====uart5

extern UART_HandleTypeDef huart5;
void Uart5_Init(uint32_t br_num)
{
    huart5.Instance = UART5;
    huart5.Init.BaudRate = br_num;
    huart5.Init.WordLength = UART_WORDLENGTH_8B;
    huart5.Init.StopBits = UART_STOPBITS_1;
    huart5.Init.Parity = UART_PARITY_NONE;
    huart5.Init.Mode = UART_MODE_TX_RX;
    huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart5.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart5) != HAL_OK)
    {
        Error_Handler();
    }

    //使能UART5接收中断
    UART5->CR1 |= USART_CR1_RXNEIE;
    //使能USART5
//    USART_Cmd(UART5, ENABLE);
//	//使能发送（进入移位）中断
    if (!(UART5->CR1 & USART_CR1_TXEIE))
    {
        UART5->CR1 |= USART_CR1_TXEIE;
    }
    __HAL_UART_ENABLE_IT(&huart5,UART_IT_ERR);
}

uint8_t Tx5Buffer[256];
uint8_t Tx5Counter = 0;
uint8_t count5 = 0;

//激光
void Uart5_IRQ(void)
{
    //接收中断
    if (UART5->SR & USART_SR_RXNE)
    {
        UART5->SR &= ~USART_SR_RXNE; //清除中断标志

        uint8_t com_data = UART5->DR;
        //====
        //Drv_Laser_GetOneByte(com_data);
        //Ultra_Get ( com_data );
    }

    //发送（进入移位）中断
    if (UART5->SR & USART_SR_TXE)
    {

        UART5->DR = Tx5Buffer[Tx5Counter++]; //写DR清除中断标志

        if (Tx5Counter == count5)
        {
            UART5->CR1 &= ~USART_CR1_TXEIE;        //关闭TXE（发送中断）中断
        }


        //USART_ClearITPendingBit(USART2,USART_IT_TXE);
    }

}

void Uart5_Send(unsigned char *DataToSend, uint8_t data_num)
{
    uint8_t i;
    for (i = 0; i < data_num; i++)
    {
        Tx5Buffer[count5++] = *(DataToSend + i);
    }

    if (!(UART5->CR1 & USART_CR1_TXEIE))
    {
        UART5->CR1 |= USART_CR1_TXEIE; //打开发送中断
    }

}